package com.example.pushmessage.controller.config;


import com.example.pushmessage.controller.RabbitMQProperties;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration
public class RabbitMQConfig {

    private static final String EXCHANGE = "wisdom_exchange_message";
    private static final String QUEUE = "wisdom_queue_message";
    private static final String ROUTINGKEY = "wisdom_routingkey_message";

    @Autowired
    private RabbitMQProperties rabbitMQProperties;

    //创建连接 : 自定义创建  可以直接使用springboot集成的mq
//    @Bean
//    public ConnectionFactory connectionFactory() {
//        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(rabbitMQProperties.getHost(),rabbitMQProperties.getPort());
//        connectionFactory.setUsername(rabbitMQProperties.getUsername());
//        connectionFactory.setPassword(rabbitMQProperties.getPassword());
//        connectionFactory.setVirtualHost(rabbitMQProperties.getVirtual_host());
//        connectionFactory.setPublisherConfirms(true);
//        return connectionFactory;
//    }
//
//    //定义RabbitTemplate  必须是prototype类型
//    @Bean
//    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
//    public RabbitTemplate rabbitTemplate() {
//        RabbitTemplate template = new RabbitTemplate(connectionFactory());
//        return template;
//    }

    //交换机
    @Bean
    public DirectExchange defaultExchange() {
        return new DirectExchange(EXCHANGE);
    }

    //队列
    @Bean
    public Queue queue() {
        return new Queue(QUEUE, true); //队列持久
    }

    @Bean
    public Binding binding() {

        return BindingBuilder.bind(queue()).to(defaultExchange()).with(ROUTINGKEY);
    }

}
